<?php


namespace App\Logic\Account\Form;

use App\Core\BaseListForm;
use App\Core\Exception\DefaultException;
use App\Model\Account\CashoutModel;
use App\Model\Member\MemberModel;
use App\Model\UserModel;
use App\Util\UserIdentityHelper;
use EasySwoole\Mysqli\QueryBuilder;

class CashoutListForm extends BaseListForm
{
    public $id;
    public $user_id;
    public $username;
    public $amout;
    public $account_type;
    public $cash_state;
    public $payment_type;
    public $created_at;
    public $start_created_at;//订单创建时间 - 开始
    public $end_created_at;//订单创建时间 - 结束


    /**
     * 充值列表
     * @return \App\Core\Output
     * @throws DefaultException
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function lists()
    {
        $where = [];
        if (UserIdentityHelper::getType() == UserModel::TYPE_INSIDE) {
            /**后台管理员*/
            if ($this->checkField($this->user_id)) {
                $where['user_id'] = $this->user_id;
            }
            if ($this->checkField($this->username)) {
                $subBuilder = QueryBuilder::subQuery();
                $subBuilder->where('username', $this->username);
                $subBuilder->get(MemberModel::create()->tableName(), null, "id");
                $where['user_id'] = [$subBuilder, 'in'];
            }
        } else {
            $this->user_id    = UserIdentityHelper::getId();
            $where['user_id'] = $this->user_id;
        }

        if ($this->checkField($this->id)) {
            $where['id'] = $this->id;
        }
        if ($this->checkField($this->account_type)) {
            $where['account_type'] = $this->account_type;
        }
        if ($this->checkField($this->cash_state)) {
            $where['cash_state'] = $this->cash_state;
        }
        if ($this->checkField($this->payment_type)) {
            $where['payment_type'] = $this->payment_type;
        }
        if ($this->checkField($this->created_at) && strtotime($this->created_at) > 0) {
            $this->created_at = date('Y-m-d', strtotime($this->created_at));
            $where['created_at'] = [[$this->created_at . ' 00:00:00', $this->created_at . ' 23:59:59'], 'between'];
        }
        /**
        订单创建时间检索
         */
        if ($this->checkField($this->start_created_at) && $this->checkField($this->end_created_at)) {
            $where['created_at'] = [[$this->start_created_at, $this->end_created_at], 'between'];
        }elseif ($this->checkField($this->start_created_at)) {
            $where['created_at'] = [$this->start_created_at, '>='];
        }elseif ($this->checkField($this->end_created_at)) {
            $where['created_at'] = [$this->end_created_at, '<='];
        }

        $model = CashoutModel::makeQueryModel($where, [], $this->getPagination());
        /**@see CashoutModel::member() */
        $model->with(['member']);
        $model->order('id', 'DESC');

        $modelResult = $model->all();

        $list = [];
        foreach ($modelResult as $v) {
            $item = [
                'id'                 => $v->id,
                'user_id'            => $v->user_id,
                'username'           => $v->member->username ?? '',
                'amout'              => bcsub($v->amout, 0, 2),
                'service_fee'        => bcsub($v->service_fee, 0, 2),
                'real_amout'         => bcsub($v->amout, $v->service_fee, 2),
                'account_type'       => $v->account_type,
                'account_type_label' => CashoutModel::getAccountTypeDict($v->account_type),
                'cash_state'         => $v->cash_state,
                'cash_state_label'   => CashoutModel::getCashStateDict($v->cash_state),
                'payment_content'    => $v->payment_content,
                'payment_type'       => $v->payment_type,
                'payment_type_label' => CashoutModel::getPaymentTypeDict($v->payment_type),
                'created_at'         => $v->created_at,
                'payment_date'       => $v->payment_date,
                'remark'             => $v->remark,
            ];
            if ($v->payment_type == CashoutModel::PAYMENT_USDT) {
                $item['usdt'] = $v->usdt;
            }
            if ($v->payment_type == CashoutModel::PAYMENT_ALIPAY) {
                $item['alipay_qr'] = $v->alipay_qr;
            }
            if ($v->payment_type == CashoutModel::PAYMENT_WECHAT) {
                $item['wepay_qr'] = $v->wepay_qr;
            }
            if ($v->payment_type == CashoutModel::PAYMENT_PUB_BANK) {
                $item['pub_bank_name']         = $v->pub_bank_name;
                $item['pub_bank_deposit']      = $v->pub_bank_deposit;
                $item['pub_bank_account_name'] = $v->pub_bank_account_name;
                $item['pub_bank_account_no']   = $v->pub_bank_account_no;
            }
            if ($v->payment_type == CashoutModel::PAYMENT_PRI_BANK) {
                $item['pri_bank_name']         = $v->pri_bank_name;
                $item['pri_bank_deposit']      = $v->pri_bank_deposit;
                $item['pri_bank_account_name'] = $v->pri_bank_account_name;
                $item['pri_bank_account_no']   = $v->pri_bank_account_no;
            }

            $list[] = $item;
        }

        $total                     = $model->lastQueryResult()->getTotalCount();
        $pagination                = $this->getPagination();
        $pagination['total_count'] = $total;

        $result = [
            'list'       => $list,
            'pagination' => $pagination
        ];
        return $result;
    }

}
